//
//  WindowDelegate.swift
//  Trans
//
//  Created by zcj on 2024/11/14.
//

import Foundation
import AppKit
import SwiftUI

class WindowDelegate:NSObject,NSWindowDelegate{
    var popupWindow:NSWindow? = nil
    func openWindow(view:some View){
        if popupWindow == nil{
            popupWindow = (NSWindow(
                contentRect: NSRect(x: 300, y: 300, width: 500, height: 400),
                styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                backing: .buffered, defer: false))
        }
        popupWindow?.isReleasedWhenClosed = false // 设置关闭后释放内存，这是必要的否则下次打开必定会出bug。否则popupWindow是不能被访问的，即使是判断也会报错
        popupWindow!.contentView = NSHostingView(rootView: view)
        popupWindow!.makeKeyAndOrderFront(nil)
        popupWindow?.delegate = self
    }
    
    override init() {
        popupWindow = (NSWindow(
            contentRect: NSRect(x: 100, y: 100, width: 500, height: 400),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false))
    }
    
    func setTitle(title:String){
        popupWindow?.title = title
    }
    
    func setSize(size:NSSize){
        popupWindow?.setContentSize(size)
    }
    
    func windowShouldClose(_ sender: NSWindow) -> Bool
    {
        print("Window should close")
        return true
    }
    
    func windowWillClose(_ notification: Notification) {
        print("Window will close")
    }
    
    func closeWindow(){
        popupWindow?.close()
    }
    
    var winDic = Dictionary<WinId,NSWindow>()
    
    static var shared:WindowDelegate = WindowDelegate()
    
    func closeWindowWithId(id:WinId){
        winDic[id]?.close()
        winDic.removeValue(forKey: id)
    }
    
    static func openWindow(view:some View,title:String,size:NSSize,id:WinId) {
        DispatchQueue.main.async{
            if(WindowDelegate.shared.winDic.keys.contains(id)){
                return
            }
            var tWindow:NSWindow? = nil
            if tWindow == nil{
                tWindow = (NSWindow(
                    contentRect: NSRect(x: 300, y: 300, width: size.width, height: size.height),
                    styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                    backing: .buffered, defer: false))
            }
            tWindow?.isReleasedWhenClosed = false // 设置关闭后释放内存，这是必要的否则下次打开必定会出bug。否则popupWindow是不能被访问的，即使是判断也会报错
            tWindow?.title = title
            tWindow!.contentView = NSHostingView(rootView: view)
            tWindow?.delegate = WindowDelegate.shared
            tWindow?.setContentSize(size)
            WindowDelegate.shared.winDic[id] = tWindow
            tWindow!.makeKeyAndOrderFront(nil)
        }
    }
    
    static func openWindowOfModal(view:some View,title:String,size:NSSize,id:WinId) {
        DispatchQueue.main.async{
            if(WindowDelegate.shared.winDic.keys.contains(id)){
                return
            }
            var tWindow:NSWindow? = nil
            if tWindow == nil{
                tWindow = (NSWindow(
                    contentRect: NSRect(x: 300, y: 300, width: size.width, height: size.height),
                    styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                    backing: .buffered, defer: false))
            }
            tWindow?.isReleasedWhenClosed = false // 设置关闭后释放内存，这是必要的否则下次打开必定会出bug。否则popupWindow是不能被访问的，即使是判断也会报错
            tWindow?.title = title
            tWindow!.contentView = NSHostingView(rootView: view)
            tWindow?.delegate = WindowDelegate.shared
            tWindow?.setContentSize(size)
            WindowDelegate.shared.winDic[id] = tWindow
            NSApplication.shared .runModal(for: tWindow!)
        }
    }
}
